package com.l024.easyandroidlib.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.hjq.toast.ToastUtils;
import com.l024.easyandroidlib.dialog.manager.DialogManager;
import com.l024.easyandroidlib.dialog.view.LoadingDialog;

import me.yokeyword.fragmentation.SupportActivity;

/**
 * @Author: Cat
 * @CreateTime: 2022-07-29  16:50
 * @Description: TODO
 * @Version: 1.0
 */
public abstract  class BaseActivity<VDB extends ViewDataBinding> extends SupportActivity{
    protected VDB mBinding;
    private LoadingDialog loadingDialog = null;
    //解决弹窗问题
    private Handler mHandler = new Handler();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this,getLayoutId());
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        hideLoading();
    }


    // 获取布局
    public abstract int getLayoutId();

    public abstract void initView();

    public abstract void initData();

    public abstract void initEvent();

    public void showLoading(){
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(this);
            DialogManager.getInstance().show(loadingDialog);
        }else {
            mHandler.removeCallbacks(runnable);
        }
    }
    public void hideLoading(){
        mHandler.postDelayed(runnable, 200);
    }

    /**
     * 直接跳转Activity
     *
     * @param activity
     * @param cls
     */
    public void go(Activity activity, Class cls) {
        Intent intent = new Intent(activity, cls);
        activity.startActivity(intent);
    }

    /**
     * 直接跳转Activity并关闭当前
     *
     * @param activity
     * @param cls
     */
    public void goAndFinish(Activity activity, Class cls) {
        Intent intent = new Intent(activity, cls);
        activity.startActivity(intent);
        activity.finish();
    }

    /**
     * 携带数据跳转Activity
     *
     * @param activity
     * @param cls
     */
    public void go(Activity activity, Class cls, Bundle bundle) {
        Intent intent = new Intent(activity, cls);
        activity.startActivity(intent, bundle);
    }

    /**
     * 携带返回值数据跳转Activity
     *
     * @param activity
     * @param cls
     */
    public void go(Activity activity, Class cls, int resultCode) {
        Intent intent = new Intent(activity, cls);
        activity.startActivityForResult(intent, resultCode);
    }

    /**
     * 携带返回值数据跳转Activity
     *
     * @param activity
     * @param cls
     */
    public void go(Activity activity, Class cls, Bundle bundle, int resultCode) {
        Intent intent = new Intent(activity, cls);
        activity.startActivityForResult(intent, resultCode, bundle);
    }

    /**
     * 封装toast方法（自行去实现）
     *
     * @param str
     */
    public void showToast(String str) {
        ToastUtils.show(str);
    }
    //解决网络加载框问题
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (loadingDialog != null && loadingDialog.isShowing()) {
                DialogManager.getInstance().hide(loadingDialog);
                loadingDialog = null;
            }
        }
    };

}
